﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using Config;

namespace MuBot
{
    public partial class ConfigWindow : Form
    {
        BotConfig botConfig = null;
        MainConfig mainConfig = null;

        public ConfigWindow()
        {
            InitializeComponent();
            mainConfig = (MainConfig)ConfigManager.ApplicationConfig;
            botConfig = (BotConfig)ConfigManager.BusinessConfig;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int temp = 0;
            if (int.TryParse(this.txtGetItemDistance.Text, out temp))
            {
                botConfig.GetItemDistance = temp;
            }
            if (int.TryParse(this.txtHP0.Text, out temp))
            {
                botConfig.HP0 = temp;
            }
            if (int.TryParse(this.txtHP1.Text, out temp))
            {
                botConfig.HP1 = temp;
            }
            if (int.TryParse(this.txtHP2.Text, out temp))
            {
                botConfig.HP2 = temp;
            }
            if (int.TryParse(this.txtMP0.Text, out temp))
            {
                botConfig.MP0 = temp;
            }
            if (int.TryParse(this.txtMP1.Text, out temp))
            {
                botConfig.MP1 = temp;
            }
            if (int.TryParse(this.txtMP2.Text, out temp))
            {
                botConfig.MP2 = temp;
            }
            if (int.TryParse(this.txtRemoveMonsterDistance.Text, out temp))
            {
                botConfig.RemoveMonsterDistance = temp;
            }
            if (int.TryParse(this.txtTimeoutLogin.Text, out temp))
            {
                botConfig.TimeoutLogin = temp;
            }
            if (int.TryParse(this.txtTimeoutMove.Text, out temp))
            {
                botConfig.TimeoutMove = temp;
            }
            if (int.TryParse(this.txtKillCount.Text, out temp))
            {
                botConfig.TimeoutKilledCount = temp;
            }
            if (int.TryParse(this.txtLine.Text, out temp))
            {
                botConfig.LineNumber = temp;
            }
            if (int.TryParse(this.txtLoginInterval.Text, out temp))
            {
                botConfig.LoginInteravl = temp;
            }
            botConfig.RandomLine = this.chkRandomLine.Checked;

            double value = 0;
            if (double.TryParse(this.txtHLBCD.Text, out value))
            {
                botConfig.HLBCD = value;
            }
            if (int.TryParse(this.txtCheckInteravl.Text, out temp))
            {
                botConfig.CheckStatusInteravl = temp;
            }
            if (int.TryParse(this.txtMaxLoginCount.Text, out temp))
            {
                botConfig.MaxLoginCount = temp;
            }
            if (int.TryParse(this.txtRefreshInteravl.Text, out temp))
            {
                botConfig.RefreshInteravl = temp;
            }
            botConfig.NeedPickupItems = this.txtNeedPickupItems.Text;
            botConfig.OnlyPickupSpecify = this.chkOnlyPickupSpecify.Checked;
            botConfig.SellExcepet = this.txtSellExcepet.Text;
            botConfig.AutoTL = this.chkAutoTL.Checked;
            botConfig.DontPickupItems = this.txtDontGetItems.Text;
            mainConfig.LogServerInfo = this.chkLogServerInfo.Checked;
            mainConfig.LogDebugInfo = this.chkLogDebug.Checked;
            try
            {
                botConfig.Save();
                mainConfig.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存配置时出错，请重试！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Config_Load(object sender, EventArgs e)
        {
            this.txtGetItemDistance.Text = botConfig.GetItemDistance.ToString();
            this.txtHP0.Text = botConfig.HP0.ToString();
            this.txtHP1.Text = botConfig.HP1.ToString();
            this.txtHP2.Text = botConfig.HP2.ToString();
            this.txtMP0.Text = botConfig.MP0.ToString();
            this.txtMP1.Text = botConfig.MP1.ToString();
            this.txtMP2.Text = botConfig.MP2.ToString();
            this.txtRemoveMonsterDistance.Text = botConfig.RemoveMonsterDistance.ToString();
            this.txtTimeoutLogin.Text = botConfig.TimeoutLogin.ToString();
            this.txtTimeoutMove.Text = botConfig.TimeoutMove.ToString();
            this.txtKillCount.Text = botConfig.TimeoutKilledCount.ToString();
            this.txtLine.Text = botConfig.LineNumber.ToString();
            this.chkRandomLine.Checked = botConfig.RandomLine;
            this.txtLoginInterval.Text = botConfig.LoginInteravl.ToString();
            this.txtHLBCD.Text = botConfig.HLBCD.ToString();
            this.txtRefreshInteravl.Text = botConfig.RefreshInteravl.ToString();
            this.txtMaxLoginCount.Text = botConfig.MaxLoginCount.ToString();
            this.txtCheckInteravl.Text = botConfig.CheckStatusInteravl.ToString();
            this.txtNeedPickupItems.Text = botConfig.NeedPickupItems;
            this.txtNeedPickupItems.Enabled = botConfig.OnlyPickupSpecify;
            this.chkOnlyPickupSpecify.Checked = botConfig.OnlyPickupSpecify;
            this.chkLogServerInfo.Checked = mainConfig.LogServerInfo;
            this.txtSellExcepet.Text = botConfig.SellExcepet;
            this.chkAutoTL.Checked = botConfig.AutoTL;
            this.txtDontGetItems.Text = botConfig.DontPickupItems;
            this.chkLogDebug.Checked = mainConfig.LogDebugInfo;
        }

        private void txtHP2_TextChanged(object sender, EventArgs e)
        {
            TextBox textbox = (TextBox)sender;
            int num = 0;
            if (!int.TryParse(textbox.Text, out num))
            {
                MessageBox.Show("请输入正整数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textbox.SelectAll();
                textbox.Focus();
            }
        }

        private void doubleText_Leave(object sender, EventArgs e)
        {
            TextBox textbox = (TextBox)sender;
            double num = 0;
            if (!double.TryParse(textbox.Text, out num))
            {
                MessageBox.Show("请输入数字！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textbox.SelectAll();
                textbox.Focus();
            }
        }

        private void chkGetAll_Click(object sender, EventArgs e)
        {
            this.txtNeedPickupItems.Enabled = this.chkOnlyPickupSpecify.Checked;
        }
    }
}
